import json

import pandas as pd
from pymongo import MongoClient

# 读取Excel文件
file_path = 'D:\\工作文档\\舌诊算法\\舌诊白话数据和精简版描述.xlsx'
df = pd.read_excel(file_path, header=0, engine='openpyxl')

# 显示前5行数据
json_data = df.to_json(orient='records')
array = json.loads(json_data)

man_common_desc_map = {}
woman_common_desc_map = {}
man_simplify_desc_map = {}
woman_simplify_desc_map = {}

for item in array:
    man_common_desc_map[item['name']] = item['manCommonDesc']
    woman_common_desc_map[item['name']] = item['womanCommonDesc']
    man_simplify_desc_map[item['name']] = item['manSimplifyDesc']
    woman_simplify_desc_map[item['name']] = item['womanSimplifyDesc']

print("男性白话解读", man_common_desc_map)
print("女性白话解读", woman_common_desc_map)
print("男性精简描述", man_simplify_desc_map)
print("女性精简描述", woman_simplify_desc_map)

# 源client
client = MongoClient('mongodb://admin:yzy%40123456@10.11.2.56:27017/')
# 源数据库
db_name = client["cm_data"]
# 源document
collection_name = db_name["shezhen_risk"]

data_list = collection_name.find()
count = 0
for item in data_list:
    print(item['name'])
    # 定义要更新的条件
    query = {"name": item['name'], 'algorithmType': 'PROFESSIONAL'}
    # 定义要更新的字段和新值
    update = {"$set": {"manCommonDesc": man_common_desc_map[item['name']],
                       "womanCommonDesc": woman_common_desc_map[item['name']],
                       "manSimplifyDesc": man_simplify_desc_map[item['name']],
                       "womanSimplifyDesc": woman_simplify_desc_map[item['name']]}}
    collection_name.update_one(query, update)
    print(update)
    count += 1
print(count)
